﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMinAndMaxWinForm
{
    public partial class 此页面无用 : Form
    {
        public 此页面无用()
        {
            InitializeComponent();
        }


        /*
       private const int WM_SIZE = 0x0005;
       const int SIZE_MAXIMIZED = 2;
       const int SIZE_MINIMIZED = 1;
       const int SC_MAXISIZE = 0xf030;//最小化  
       protected override void DefWndProc(ref Message m)
       {
            
           switch (m.Msg)
           {
               case WM_SIZE:
                   if (m.WParam.ToInt32() == SIZE_MAXIMIZED)
                   {
                       // 窗体最大化
                      // this.WindowState = FormWindowState.Maximized;
                       MessageBox.Show("Maxmized");
                       Console.WriteLine("Maxmized");
                   }
                   else if (m.WParam.ToInt32() == SIZE_MINIMIZED)
                   {
                      
                       // 窗体最小花
                      // LocatePosition();
                       //this.WindowState = FormWindowState.Minimized;
                       MessageBox.Show("Minimized");
                       Console.WriteLine("Minimized");
                   }
                   else
                   {
                       //其他
                      // this.WindowState = FormWindowState.Maximized;
                       //MessageBox.Show("Other");
                       base.DefWndProc(ref m);
                   }
                   break;
               default:
                   base.DefWndProc(ref m);
                   break;
           }
       }
       */

        private static Message myFormMessage;
        protected override void WndProc(ref Message m)
        {
            myFormMessage = m;
            const int WM_SYSCOMMAND = 0x112;
            const int SC_CLOSE = 0xf060;//关闭  
            const int SC_MINSIZE = 0xf020;//最大化  
            const int SC_MAXISIZE = 0xf030;//最小化  
            const int SC_NORMAL = 0xf120;//还原  
            const int SC_DOUBLECLICK = 0xf122;//双击窗体标题栏  
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_CLOSE))//关闭  
            {

                MessageBox.Show("关闭");
                // return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_MAXISIZE))//最大化  
            {
                this.WindowState = FormWindowState.Maximized;
                //MessageBox.Show("最大化");
                this.Show();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_MINSIZE))//最小化  
            {


                LocatePosition2();
                MessageBox.Show("最小化");
                //this.Hide();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_NORMAL))//还原  
            {

                MessageBox.Show("还原");

                this.Show();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_DOUBLECLICK))///双击窗体标题栏  
            {
                MessageBox.Show("双击窗体标题栏 ");
                return;
            }
            //if (m.Msg == WM_SYSCOMMAND)//用来获取用户触发事件的16进制参数，通过科学计算器转换  
            //{  
            //    int test = (int)m.WParam;  
            //}  
            base.WndProc(ref m);
        }

        private void LocatePosition()
        {
            Rectangle r = Screen.GetWorkingArea(this);
            this.Location = new Point(r.Right - this.Width, r.Bottom - this.Height);
            this.TopMost = true;
        }

        private void LocatePosition2()
        {
            this.WindowState = FormWindowState.Normal;
            this.Width = 180;
            this.Height = 30;
            Rectangle r = Screen.GetWorkingArea(this);
            this.Location = new Point(r.Right - this.Width, r.Bottom - this.Height);
            this.TopMost = true;

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            const int WM_SYSCOMMAND = 0x112;
            const int SC_CLOSE = 0xf060;//关闭  
            const int SC_MINSIZE = 0xf020;//最大化  
            const int SC_MAXISIZE = 0xf030;//最小化  
            const int SC_NORMAL = 0xf120;//还原  
            const int SC_DOUBLECLICK = 0xf122;//双击窗体标题栏  

            if ((myFormMessage.Msg == WM_SYSCOMMAND) && ((int)myFormMessage.WParam == SC_MINSIZE))//最小化  
            {
                MessageBox.Show("Resize Miniseze");
                this.Width = 100;
                this.Height = 100;
            }


        }  
    }
}
